package mygui

import (
    "fmt"
    //"os"
    //"strings"
    //"reflect"
    //"image/color"
    "fyne.io/fyne/v2"
    //"fyne.io/fyne/v2/app"
    "fyne.io/fyne/v2/theme"
    "fyne.io/fyne/v2/widget"
    //"fyne.io/fyne/v2/canvas"
    //"github.com/flopp/go-findfont"
    "fyne.io/fyne/v2/container"
    "fyne.io/fyne/v2/layout"
    //"fyne.io/fyne/v2/data/binding"
    //"encoding/json"
    //"net/http"
    //"io/ioutil"
    //"net/url"
)
/*
func init() {
    fontPaths := findfont.List()
    for _, path := range fontPaths {
        //fmt.Println(path)
        //楷体:simkai.ttf
        //黑体:simhei.ttf
        if strings.Contains(path, "simkai.ttf") {
            fmt.Println(path)
            os.Setenv("FYNE_FONT", path) // 设置环境变量  // 取消环境变量 os.Unsetenv("FYNE_FONT")
            break
        }
    }
    fmt.Println("=============")
}
*/

type myLayout struct {
    horizontal bool
}

func (g *myLayout) isSpacer(obj fyne.CanvasObject) bool {
    // invisible spacers don't impact layout
    if !obj.Visible() {
        return false
    }

    if g.horizontal {
        return isHorizontalSpacer(obj)
    }
    return isVerticalSpacer(obj)
}
type SpacerObject interface {
    ExpandVertical() bool
    ExpandHorizontal() bool
}

func isHorizontalSpacer(obj fyne.CanvasObject) bool {
    if spacer, ok := obj.(SpacerObject); ok {
        return spacer.ExpandHorizontal()
    }

    return false
}

func isVerticalSpacer(obj fyne.CanvasObject) bool {
    if spacer, ok := obj.(SpacerObject); ok {
        return spacer.ExpandVertical()
    }

    return false
}

func (g *myLayout) Layout(objects []fyne.CanvasObject, size fyne.Size) {
    spacers := make([]fyne.CanvasObject, 0)
    total := float32(0)
    for _, child := range objects {
        if !child.Visible() {
            continue
        }

        if g.isSpacer(child) {
            spacers = append(spacers, child)
            continue
        }
        if g.horizontal {
            total += child.MinSize().Width
        } else {
            total += child.MinSize().Height
        }
    }

    x, y := float32(0), float32(0)
    var extra float32
    if g.horizontal {
        extra = size.Width - total - (theme.Padding() * float32(len(objects)-len(spacers)-1))
    } else {
        extra = size.Height - total - (theme.Padding() * float32(len(objects)-len(spacers)-1))
    }
    extraCell := float32(0)
    if len(spacers) > 0 {
        extraCell = extra / float32(len(spacers))
    }
    for _, child := range objects {
        if !child.Visible() {
            continue
        }

        height := child.MinSize().Height

        if g.isSpacer(child) {

            if g.horizontal {
                x += extraCell
            } else {
                y += extraCell
            }
            continue
        }
        child.Move(fyne.NewPos(x, y))
        y += theme.Padding() + (size.Height - height*3)
        child.Resize(fyne.NewSize(size.Width, size.Height - height*3))

    }
}

func (g *myLayout) MinSize(objects []fyne.CanvasObject) fyne.Size {
    minSize := fyne.NewSize(0, 0)
    addPadding := false
    for _, child := range objects {
        if !child.Visible() {
            continue
        }

        if g.isSpacer(child) {
            continue
        }

        if g.horizontal {
            minSize.Height = fyne.Max(child.MinSize().Height, minSize.Height)
            minSize.Width += child.MinSize().Width
            if addPadding {
                minSize.Width += theme.Padding()
            }
        } else {
            minSize.Width = fyne.Max(child.MinSize().Width, minSize.Width)
            minSize.Height += child.MinSize().Height
            if addPadding {
                minSize.Height += theme.Padding()
            }
        }
        addPadding = true
    }
    return minSize
}

func See_main(myApp fyne.App, t_data chan int) fyne.Window  {
    var data = [][]string{
        []string{"渠道", "新增","消费"},
        []string{"游戏Fan","",""},
        []string{"联想","",""},
        []string{"九游","",""},
        []string{"一元","",""},
    }
    //myApp := app.New()
    myWindow := myApp.NewWindow("查询界面")
    //var m_data = make(chan string)

    //m_data := binding.BindStringList(&data,)
    list := widget.NewTable(
        func() (int, int) {
            return len(data), len(data[0])
        },
        func() fyne.CanvasObject {
            mwidget := widget.NewLabel("wide content")
            return mwidget
        },
        func(i widget.TableCellID, o fyne.CanvasObject) {
            o.(*widget.Label).SetText(data[i.Row][i.Col])
            //o.Resize(fyne.NewSize(20, 18))
        })
    input  := widget.NewEntry()
    input.SetPlaceHolder("输入游戏名字...")
    button := widget.NewButton("查询", func() {
        //m_data <- input.Text
        my_data_see(data, input.Text,1,1)
        list.Refresh()
    })
    contentinput := container.New(layout.NewVBoxLayout(),input,button)
    //contentinput.Resize(fyne.NewSize(500, 300))
    //max := container.New(&myLayout{},list)
    box := container.New(&myLayout{},list,contentinput)
    myWindow.SetContent(box)
    myWindow.SetCloseIntercept(func(){
        fmt.Println("禁止关闭窗口")
        //myWindow.Hide()
        t_data <- 5
    })
    //go my_data_see(myWindow,contentinput,m_data)
    //myWindow.Resize(fyne.NewSize(320, 250))
    //myWindow.CenterOnScreen()
    //myWindow.ShowAndRun()
    //os.Unsetenv("FYNE_FONT")
    return myWindow
}


func my_data_see(datasd [][]string, text string, x int, y int) bool {
    if x == 5 {
        return true
    }
    data, err := http_data(5, text,datasd[x][0],datasd[0][y])
    if err == nil {
        datasd[x][y] = data
    }else{
        return false
    }
    if y == 2 {
        return my_data_see(datasd, text, x+1, 1)
    }else{
        return my_data_see(datasd, text, x, 2)
    }
   
}


